#include  "Typedef.h"
#include  "macros.h"


#include  "Phy_Frame.h"
#include  "UartProtocol.h"
#include  "Wirelessprotocol.h"
#include  "WirelessComm.h"
#include  "SysComm.h"
#include  "eSeal_Management.h"



SYS_COMM_STATUS_WORD  SysCommStatus;

extern  WIRELESS_COMM_CONTROL WirelessControlWord;
extern  READER_PROPERTY  ReaderProperty;
extern  WIRELESSPROTOCOL_P2P_UP_FRAME  RecvFrame;

INT8U  System_Comm(UART_RX_FRAME* pUartFrame)
{
    INT8U  err;
    do{
        switch (SysCommStatus.CommStatus)
        {
        case  SYS_COMM_NONE:
          {
            SysCommStatus.CommStatus = SYS_COMM_WIRELESS_END;
        
          }
          break;
        case  SYS_COMM_START:
            if (WirelessControlWord.bUHFCommEstablished)
            {
                SysCommStatus.CommStatus = SYS_COMM_WIRELESS_START;
            }
            else
            {
                if (WIRELESS_COMM_LF_WAKE_UP_OK == WirelessCommLFWakeUp())
                {
                    SysCommStatus.CommStatus = SYS_COMM_WIRELESS_START;
                    WirelessComm_TimerStart();
                }
                else
                {
                    SysCommStatus.CommStatus = SYS_COMM_WIRELESS_ERR;
                    err = SYS_COMM_ERR_LFWAKE;
                }
            }
            break;
        case  SYS_COMM_WIRELESS_START:
            {
                INT8U  Cmd = pUartFrame->Data[UARTFRAME_DATA_POS_CMD];
                ESEAL_DETAIL* peSeal;
            
                peSeal = eSeal_Get_eSealObj();

                if(peSeal)
                {               
                    switch (Cmd)
                    {
                    case UARTPROTOCOL_CMD_RD_ESEAL_ID:
                        WIRELESS_COMM_COMMAND(WIRELESSPROTOCOL_P2P_CMD_READ_PARA,
                                              eSeal_SendCmd_ReadPara(peSeal->VendorID , peSeal->eSealID , CMD_READ_PARA_OPTION_ESEALID , &err)
                                              );
                        if(WirelessComm_IsCmdSuccess())
                        {
                            SysCommStatus.CommStatus = SYS_COMM_WIRELESS_PARSE;
                        }    
                        else
                        {
                            SysCommStatus.CommStatus = SYS_COMM_WIRELESS_ERR;
                            err = SYS_COMM_WIRELESS_ERR;
                        }
                        break;
                    
                    case UARTPROTOCOL_CMD_RD_ESEAL_EVENTNUM:
                        WIRELESS_COMM_COMMAND(WIRELESSPROTOCOL_P2P_CMD_READ_EVENT_NUM,
                                              eSeal_SendCmd_ReadEventNum(peSeal->VendorID , peSeal->eSealID , &err)
                                              );
                        if(WirelessComm_IsCmdSuccess())
                        {
                            SysCommStatus.CommStatus = SYS_COMM_WIRELESS_PARSE;
                        }    
                        else
                        {
                            SysCommStatus.CommStatus = SYS_COMM_WIRELESS_ERR;
                            err = SYS_COMM_WIRELESS_ERR;
                        }
                        break;
                    case UARTPROTOCOL_CMD_RD_ESEAL_EVENT:
                        {
                            INT16U  RecordNum = *(INT16U*)(&pUartFrame->Data[UARTFRAME_DATA_POS_CMD_OPTION]);
                            WIRELESS_COMM_COMMAND(WIRELESSPROTOCOL_P2P_CMD_READ_EVENT_RECORD,
                                                  eSeal_SendCmd_ReadEventRecord(peSeal->VendorID , peSeal->eSealID , RecordNum , &err)
                                                 );
                            if(WirelessComm_IsCmdSuccess())
                            {
                                SysCommStatus.CommStatus = SYS_COMM_WIRELESS_PARSE;
                            }    
                            else
                            {
                                SysCommStatus.CommStatus = SYS_COMM_WIRELESS_ERR;
                                err = SYS_COMM_WIRELESS_ERR;
                            }
                        }
                        break;
                    case UARTPROTOCOL_CMD_RD_ESEAL_RTC:
                        {
                            WIRELESS_COMM_COMMAND(WIRELESSPROTOCOL_P2P_CMD_READ_RTC,
                                eSeal_SendCmd_ReadRTC(peSeal->VendorID , peSeal->eSealID , &err)
                                );
                            if(WirelessComm_IsCmdSuccess())
                            {
                                SysCommStatus.CommStatus = SYS_COMM_WIRELESS_PARSE;
                            }    
                            else
                            {
                                SysCommStatus.CommStatus = SYS_COMM_WIRELESS_ERR;
                                err = SYS_COMM_WIRELESS_ERR;
                            }
                        }
                        break;
                    case UARTPROTOCOL_CMD_SET_INOUT:
                        {
                            WIRELESS_COMM_COMMAND(WIRELESSPROTOCOL_P2P_CMD_PORT_INOUT,
                                                  eSeal_SendCmd_PortInOut(peSeal->VendorID , peSeal->eSealID , ReaderProperty.InOut , &err)
                                                  );
                            if(WirelessComm_IsCmdSuccess())
                            {
                                eSeal_SendCmd_Sleep(peSeal->VendorID , peSeal->eSealID , &err);
                                eSeal_SendCmd_Sleep(peSeal->VendorID , peSeal->eSealID , &err);
                                eSeal_SendCmd_Sleep(peSeal->VendorID , peSeal->eSealID , &err);
                                SysCommStatus.CommStatus = SYS_COMM_WIRELESS_PARSE;
                            }    
                            else
                            {
                                SysCommStatus.CommStatus = SYS_COMM_WIRELESS_ERR;
                                err = SYS_COMM_WIRELESS_ERR;
                            }
                        }
                        break;
                    case UARTPROTOCOL_CMD_OPERAT_OVER_READER:
                         eSeal_SendCmd_Sleep(peSeal->VendorID , peSeal->eSealID , &err);
                         SysCommStatus.CommStatus = SYS_COMM_WIRELESS_END;
                         break;
                    default:
                        SysCommStatus.CommStatus = SYS_COMM_WIRELESS_ERR;
                        err = SYS_COMM_WIRELESS_ERR;
                        break; 
                    }
                }              
            }
            break;
        case  SYS_COMM_WIRELESS_PARSE:
            if (ParseP2PPacket(&RecvFrame, &err) == FRAME_PARSE_OK)
            {
               err = SYS_COMM_NO_ERR;
               SysCommStatus.IsWirelessCommSuccess = TRUE;
               SysCommStatus.CommStatus = SYS_COMM_WIRELESS_END;
            }
            else
            {
                SysCommStatus.CommStatus = SYS_COMM_WIRELESS_PARSE_ERR;
                err = SYS_COMM_WIRELESS_ERR;
            }
            break;
        case  SYS_COMM_WIRELESS_END:
            SysCommStatus.CommStatus = SYS_COMM_WIRELESS_END;
            break;
        case  SYS_COMM_WIRELESS_ERR:
            break;
        case SYS_COMM_WIRELESS_PARSE_ERR:
            break;
        case  SYS_COMM_END:
            err = SYS_COMM_NO_ERR;
            SysCommStatus.IsWirelessCommSuccess = TRUE;
            break;
        default:
            break;    
        }
  } while(((SysCommStatus.CommStatus != SYS_COMM_WIRELESS_END)&&
          (SysCommStatus.CommStatus != SYS_COMM_WIRELESS_ERR)&&
          (SysCommStatus.CommStatus != SYS_COMM_WIRELESS_PARSE_ERR)));
    return err;
}

